package com.songtech.set.controller;


import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.login.model.SysPosUser;
import com.songtech.position.controller.PositionController;
import com.songtech.set.service.SetService;
import com.songtech.web.entry.Setting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

/**
 * 
 * @author zhaoxulin
 * 
 *
 */

@Controller
@RequestMapping("/set/")
public class SetController {

	private Logger logger = LoggerFactory.getLogger(SetController.class);

	@Autowired
	private  SetService   setService;
	
	/**
	 * 根据imei查询设置表中的数据
	 * @param imei
	 * @return
	 */
	@RequestMapping(value="selectSetting")
	@ResponseBody
	public JsonResult selectSetting(HttpSession session){
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		System.out.println(user);
		String imei=user.getImei();
		
		//String imei="865205033247320";		
		Setting setting=null;
		try {
			 setting=setService.selectSettings(imei);
		} catch (SQLException e) {
			logger.error("查询设置数据异常信息:"+e.getMessage(),e);
			return  new  JsonResult(setting, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return  new  JsonResult(setting, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}

	/**
	 * 设置模式(定位时间间隔)
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "setPostTimeInterval",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public   JsonResult   setPostTimeInterval(Setting bean,HttpSession session){
		System.out.println(bean.getPostimeInterval());
		/**
		 * 从客户信息中获取imei
		 */
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		String imei=user.getImei();	
		//String imei="865205033247320";
		bean.setImei(imei);
		boolean   flag = setService.setPostTimeInterval(bean);
		if(!flag){
			return  new  JsonResult(flag, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			
		}
		return  new  JsonResult(flag, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}


	/**
	 * 设置短信告警电话
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "setMsgPhonenum")
	@ResponseBody
	public   JsonResult  setMsgPhonenum(Setting bean,HttpSession session){
		System.out.println(bean.getMsgPhonenum());
		/**
		 * 从客户信息中获取imei
		 */
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		String imei=user.getImei();
		//String imei="865205033247320";
		bean.setImei(imei);
		boolean  flag = setService.setMsgPhonenum(bean);
		if(flag){
			return  new  JsonResult(flag, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
		}
		return  new  JsonResult(flag, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
	}
	

	/**
	 * 设置告警电话
	 * @param bean
	 * @return
	 */
	@RequestMapping(value = "setCallPhonenum",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public   JsonResult  setCallPhonenum(Setting bean,HttpSession session){
		
		
		/**
		 * 从客户信息中获取imei
		 */
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		String imei=user.getImei();
		//String imei="865205033247320";
		bean.setImei(imei);
		boolean  flag = setService.setCallPhonenum(bean);
		
		if(flag){
			return  new  JsonResult(flag, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
		}
		return  new  JsonResult(flag, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
	}

	
	/**
	 * 远程关机
	 * @param bean
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="pwoff")
	public JsonResult pwoff(Setting bean,HttpServletRequest request){
		SysPosUser user = (SysPosUser)request.getSession().getAttribute("loginUser");
		String imei = user.getImei();
		//String imei="865205033247320";
		bean.setImei(imei);
		boolean flag = setService.pwoff(bean);
		if(flag){
			return new JsonResult(flag, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
		}
		return new JsonResult(flag, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
	}

	
}
